<?php
namespace app\wadmin\controller;
// +----------------------------------------------------------------------
// | bbcThink
// +----------------------------------------------------------------------
// | Copyright (c) 2025 http://bbcweb.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: bbcweb <22745529@qq.com>
// +----------------------------------------------------------------------
use think\facade\Db;
use app\BaseController;
use think\facade\View;
use app\service\MenuService;

class Menu extends BaseController
{
    protected $table='menu';
    public function index()
    {
        $p=input();
        $where=array();
        if(!empty($p['keyword']))
        {
            $where[]=['name','like','%'.$p['keyword'].'%'];
        }
        //$where[]=['id','not in',[1,2,3,5,74,90,94,85]];
        $category = MenuService::getTreeList(['where'=>$where]);
        View::assign('list',$category);
        return View::fetch();
    }
    public function default()
    {
        $p=input();
        $where=array();
        if(!empty($p['keyword']))
        {
            $where[]=['name','like','%'.$p['keyword'].'%'];
        }
        $category = MenuService::getTreeList($where);
        View::assign('list',$category);
        return View::fetch();
    }
    public function add()
    {
        $p=input();
        if($this->request->ispost())
        {
            $data=$p;
            if(empty($p['pid']))
            {
                $data['pid']=0;
            }
            else
            {
                $pmenu=Db::name('menu')->where('id','=',$p['pid'])->find();
                $data['level']=$pmenu['level']+1;

            }
            $res=Db::name('menu')->insert($data);
            return jsonRes($res);
        }
        $menus=array();
        $menus=MenuService::getTreeList(['where'=>[]]);
        if(!empty($p['pid']))
        {
            View::assign('vo',['pid'=>$p['pid']]);
        }
        View::assign('menus',$menus);
        return View::fetch();
    }
    public function form()
    {
        $p=input();
        $category=array();
        $pid=0;
        if($this->request->isPost())
        {
            unset($p['file']);
            //$p['status'] = empty($p['status'])?0:1;
            if(intval($p['pid'])==$p['id'])
            {
                return jsonReturn(0,'父级ID不能是自己');
            }
            $p['level']=0;
            if(empty($p['id']))
            {
                $pmenu=Db::name('menu')->where('id','=',$p['pid'])->find();                            
                $p['level']=$pmenu['level']+1;
            }
            $res = Db::name($this->table)->where(['id'=>intval($p['id'])])->update($p);
           
            return jsonReturn(1,$res,$p);
        }
        $data = Db::name($this->table)->where(['id'=>intval($p['id'])])->find();    
        $menus=Db::name('menu')->where('pid',0)->select()->toarray();//MenuService::getTreeList();   
        View::assign('vo',$data);        
        View::assign('menus',$menus);
        View::assign('pid',$pid);
        return View::fetch();
    }
   
    public function status()
    {
        $p = input();
        $res = Db::name($this->table)->where(['id'=>intval($p['id'])])->update(['status'=>intval($p['status'])]);
        return jsonRes($res);
    }
    public function setsort()
    {
        $p = input();
        $res = Db::name($this->table)->where(['id'=>intval($p['id'])])->update(['sort'=>intval($p['sort'])]);
        return jsonRes($res);
    }
    public function remove()
    {
        $p = input();       
        if($p['leve']==2)
        {
            Db::query("delete from lb_menu where id=".$p['id']);
        }
        if($p['leve']==1)
        {
            Db::query("delete from lb_menu where id=".$p['id']);
            $sql="delete from lb_menu where pid=".$p['id'];
            //return $sql;
            Db::query($sql);
        }
        if($p['leve']==0)
        {
            $sql="delete from lb_menu where pid in(select a.pid from (select pid from lb_menu where pid=".$p['id'].") a)";           
            Db::query($sql);
            Db::query("delete from lb_menu where pid=".$p['id']);
            Db::query("delete from lb_menu where id=".$p['id']);
        }
        return jsonRes(1);
    }
    public function removeall()
    {
        $p=input();
        // foreach(explode(',',$p['id']) as $v)
        foreach($p['id'] as $v)
        {
            $sql="delete from lb_menu where pid in(select a.pid from (select pid from lb_menu where pid=".$v.") a)";           
            Db::query($sql);
            Db::query("delete from lb_menu where pid=".$v);
            Db::query("delete from lb_menu where id=".$v);
        }
        return jsonRes(1);
    }
    public function icon()
    {
        return View::fetch();
    }
}